/** An item that can possibly be picked up, used and equiped.
 */
function Item()
{
	this.name = "Item";
	this.character = "?";
	this.fg = Color.white;
	this.bg = null;
	this.itemType = "none";
	this.weight = 1;
	this.mods = {}
	this.spell = null;
	this.charges = 0;
	this.destroyOnOutOfCharges = false;
}
Item.weaponTypes = ["1h", "2h"];
Item.equipableTypes = ["1h", "2h", "head", "body", "arms", "hands", "legs", "feet"];
Item.equipFlags = {
	"1h": "W",
	"2h": "W",
	"head": "H",
	"body": "B",
	"arms": "A",
	"hands": "h",
	"legs": "L",
	"feet": "F"
};
Item.prototype = new Entity("item", "?", null, null, false, false);
Item.prototype.isItem = true;
/** Create a new item by its ID.
 *@param id The ID of the item.
 */
Item.newById = function(id)
{
	var ret = new Item();
	ret.init.call(ret, items[id]);
	ret.__baseId = id;
	return ret;
}
/** Initialization routine for item properties.
 *
 * All members of bag may be an array of possbile values.
 *@param name The name of the item.
 *@param c The character used to represent this item.
 *@param fg The foreground color.
 *@param bg The background color.
 *@param itemType The type of item, such as melee, potion, scroll, ring, etc.
 *@param weight The weight of the item in pounds.
 *@param str The strength required to equip this item without a penalty.
 *@param mods The mobile property modifications that this item has. This is a property back of all of the properties that will be changed and a numeric value that will be ADDED to that property.
 *@param spell The spell that can be cast by using this item.
 *@param spellParam A parameter bag for the spell effect. Not all spells use this.
 *@param charges The number of spell charges the item has.
 *@param chargesMax The maximum number of spell charges the item can have.
 *@param destroyOnOutOfCharges Should the item be destroyed when it is out of charges?
 */
Item.prototype.init = function(bag)
{
	util.mix(this, bag);
}
/** Use a useable item.
 *@param source The source using the item.
 *@param target The target of the item.
 */
Item.prototype.use = function(source, target)
{
	var spell = this.getSpell();
	spell.apply(source, target);
	if(source.isMobile)
		source.statCheck();
	if(target.isMobile)
	{
		target.wasHitBy(source);
		target.statCheck();
	}
	if(this.hasOwnProperty("charges"))
	{
		--this.charges;
		if(this.charges <= 0 &&
			this.destroyOnOutOfCharges)
			this.parent.removeEntity(this);
	}
}
/** Tells us if we can use this item.
 *@param source The mobile trying to use the item.
 *@param doMessage If true we will print a message saying why we can't use the item.
 */
Item.prototype.canUse = function(source, doMessage)
{
	if(this.hasOwnProperty("charges") &&
		this.charges <= 0)
	{
		if(doMessage)
			dhack.logWindow.pushMessage("%b0This item is out of charges.%f0");
		return false;
	}
	return true;
}
Item.prototype.getSpell = function()
{
	return Spell.newById(this.spell, this.spellParam);
}
Item.prototype.getUsableEffectsString = function(source)
{
	var spell = this.getSpell();
	var e = spell.getEffects(source);
	var totalMods = {};
	for(var i = 0; i < e.length; ++i)
	{
		util.addMix(totalMods, e[i].mods);
	}
	var ret = Mobile.getModEffectString(totalMods);
	if(this.hasOwnProperty("charges") &&
		this.chargesMax > 1)
	{
		ret += " (" + this.charges + "/" + this.chargesMax + ")";
	}
	return ret;
}
Item.prototype.isWeapon = function()
{
	if(!this.hasOwnProperty("itemType"))
		return false;
	for(var i = 0; i < Item.weaponTypes.length; ++i)
	{
		if(this.itemType == Item.weaponTypes[i])
			return true;
	}
	return false;
}
Item.prototype.isEquipable = function()
{
	if(!this.hasOwnProperty("itemType"))
		return false;
	for(var i = 0; i < Item.equipableTypes.length; ++i)
	{
		if(this.itemType == Item.equipableTypes[i])
			return true;
	}
	return false;
}
Item.prototype.getEquipFlag = function()
{
	if(this.hasOwnProperty("itemType") &&
		Item.equipFlags.hasOwnProperty(this.itemType))
		return Item.equipFlags[this.itemType];
	return "?";
}
ron.registerCtor(Item, "Item");
